package com.example.expandablelistview

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat

/**
 * @author by liliang on 2020/5/31 8:24 PM
 */
class ExpandableListViewAdapter(
    private var groups: Array<String>, private var childs: Array<Array<String>>,
    private var context: Context) : BaseExpandableListAdapter()  {

    override fun getGroupCount() = groups.size

    override fun getChildrenCount(groupPosition: Int) = childs[groupPosition].size

    override fun getGroup(groupPosition: Int) = groups[groupPosition]

    override fun getChild(groupPosition: Int, childPosition: Int) = childs[groupPosition][childPosition]

    override fun getGroupId(groupPosition: Int) = groupPosition.toLong()

    override fun getChildId(groupPosition: Int, childPosition: Int) = childPosition.toLong()
    //指定位置上的子元素是否可选中
    override fun isChildSelectable(groupPosition: Int, childPosition: Int) = true
    //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    override fun hasStableIds() = true

    override fun getGroupView(groupPosition: Int, isExpanded: Boolean,
                              convertView: View?, parent: ViewGroup?): View {
        val groupViewHolder: GroupViewHolder
        val view: View
        if (convertView != null) {
            groupViewHolder = convertView.tag as GroupViewHolder
            view = convertView
        } else {
            view = LayoutInflater.from(parent?.context)
                .inflate(R.layout.parent, parent, false)
            groupViewHolder = GroupViewHolder().apply {
                parentItem = view.findViewById(R.id.parent_text_view)
                parentIcon = view.findViewById(R.id.parent_icon)
            }
            view.tag = groupViewHolder
        }
        groupViewHolder.parentItem.text = groups[groupPosition]
        if (isExpanded) {
            groupViewHolder.parentIcon.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_down_arrow))
        } else {
            groupViewHolder.parentIcon.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_right_arrow))
        }
        return view
    }

    override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean,
                                                     convertView: View?, parent: ViewGroup?): View {
        val childViewHolder: ChildViewHolder
        val view: View
        if (convertView != null) {
            childViewHolder = convertView.tag as ChildViewHolder
            view = convertView
        } else {
            view = LayoutInflater.from(parent?.context)
                .inflate(R.layout.child, parent, false)
            childViewHolder = ChildViewHolder().apply {
                childItem = view.findViewById(R.id.children_item)
            }
            view.tag = childViewHolder
        }
        childViewHolder.childItem.text = childs[groupPosition][childPosition]
        return view
    }


    inner class GroupViewHolder {
        lateinit var parentItem : TextView
        lateinit var parentIcon : ImageView
    }

    inner class ChildViewHolder {
        lateinit var childItem : TextView
    }


}